package cn.tansanqinger.service.announcement;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tansanqinger.dao.sql.announcement.AnnouncementSqlDaoImpl;
import cn.tansanqinger.pojo.Admin;
import cn.tansanqinger.pojo.Announcement;
import cn.tansanqinger.util.DateUtil;
import cn.tansanqinger.util.PageData;
import cn.tansanqinger.util.Tools;

@Service
public class AnnouncementServiceImpl implements IAnnouncementService{
	
	@Autowired
	private AnnouncementSqlDaoImpl impl;

	@Override
	public PageData selectAnnouncement(HttpServletRequest request) {
		String keywords = Tools.isEmpty(request.getParameter("keywords"));
		String time = Tools.isEmpty(request.getParameter("time"));
		String page = Tools.isEmpty(request.getParameter("page"));
		String limit = Tools.isEmpty(request.getParameter("limit"));
		int pageInt = 1;
		if(!"".equals(page)) {
			pageInt = Integer.parseInt(page);
		}
		int limitInt = 10;
		if(!"".equals(limit)) {
			limitInt = Integer.parseInt(limit);
		}
		long startTime = -1;
		long endTime = -1;
		if(!"".equals(time)) {
			startTime = DateUtil.getTime(time.split("\\-")[0]);
			endTime = DateUtil.getTime(time.split("\\-")[1]);
		}
		return impl.selectAnnouncement(keywords, startTime, endTime, pageInt, limitInt);
	}

	@Override
	public int announcementNumber(HttpServletRequest request) {
		return impl.serialNumber();
	}

	@Override
	public PageData add(HttpServletRequest request) {
		Admin admin = (Admin) request.getSession().getAttribute("userMessage");
		Announcement announcement = new Announcement();
		announcement.setAdate(DateUtil.getTime());
		announcement.setAmessage(Tools.isEmpty(request.getParameter("message")));
		announcement.setAsn(impl.serialNumber());
		announcement.setAtime(System.currentTimeMillis()/1000);
		announcement.setAtitle(Tools.isEmpty(request.getParameter("title")));
		announcement.setAudiences(Integer.parseInt(Tools.isEmpty(request.getParameter("audiences"))));
		announcement.setUid(admin.getAid());
		PageData resultMap = new PageData();
		if(impl.add(announcement)==1) {
			resultMap.put("code","0");
			resultMap.put("msg", "添加成功");
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "添加失败");
		}
		return resultMap;
	}

	@Override
	public PageData modification(HttpServletRequest request) {
		PageData resultMap = new PageData();
		Announcement announcement = selectAnnouncementOne(request);
		if(announcement!=null) {
			announcement.setAdate(DateUtil.getTime());
			announcement.setAmessage(Tools.isEmpty(request.getParameter("message")));
			announcement.setAtime(System.currentTimeMillis()/1000);
			announcement.setAtitle(Tools.isEmpty(request.getParameter("title")));
			announcement.setAudiences(Integer.parseInt(Tools.isEmpty(request.getParameter("audiences"))));
			if(impl.modification(announcement)==1) {
				resultMap.put("code","0");
				resultMap.put("msg", "修改成功");
			} else {
				resultMap.put("code","-1");
				resultMap.put("msg", "修改失败");
			}
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "数据异常");
		}
		return resultMap;
	}

	@Override
	public PageData delete(HttpServletRequest request) {
		PageData resultMap = new PageData();
		String id = Tools.isEmpty(request.getParameter("id"));
		if(id.matches("\\d+")) {
			if(impl.deleteOne(Integer.parseInt(id))==1) {
				resultMap.put("code","0");
				resultMap.put("msg", "删除成功");
			} else {
				resultMap.put("code","-1");
				resultMap.put("msg", "删除失败");
			}
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "数据错误");
		}
		return resultMap;
	}

	@Override
	public Announcement selectAnnouncementOne(HttpServletRequest request) {
		String id = Tools.isEmpty(request.getParameter("id"));
		if(id.matches("\\d+")) {
			return impl.selectOnt(Integer.parseInt(id));
		}
		return null;
	}
	
	

}
